# 商品详情页


# 定义对象库层
import allure
from selenium.webdriver.common.by import By

from tshop.base.base import BasePage, BaseHandle
from tshop.utils import UtilsDriver


class GoodsDetailPage(BasePage):
    def __init__(self):
        # 初始化
        super().__init__()
        # 搜索之后加入购物车按钮
        self.search_add_btn = By.CSS_SELECTOR, ".p-btn a"
        # 详情页加入购物车按钮
        self.add_btn = By.CSS_SELECTOR, "#join_cart"
        # 获取iframe
        self.iframe = By.CSS_SELECTOR, "#layui-layer-iframe1"
        # iframe信息
        self.iframe_msg = By.CSS_SELECTOR, ".conect-title span"

    # 获取第一个购物车按钮
    def find_search_add_btn(self):
        return self.get_element(self.search_add_btn)

    # 获取第二个购物车按钮
    def find_add_btn(self):
        return self.get_element(self.add_btn)

    # 获取iframe
    def find_iframe(self):
        return self.get_element(self.iframe)

    # 获取iframe信息
    def find_iframe_msg(self):
        return self.get_element(self.iframe_msg)


# 定义操作层
class GoodsDetailHandle(BaseHandle):
    def __init__(self):
        self.goods_detail_page = GoodsDetailPage()
    # 点击第一个购物车按钮
    @allure.step(title="点击第一个购物车按钮")
    def click_search_add_btn(self):
        self.goods_detail_page.find_search_add_btn().click()

     # 点击第二个购物车按钮
    @allure.step(title="点击第二个购物车按钮")
    def click_add_btn(self):
        self.goods_detail_page.find_add_btn().click()
    #切换到iframe
    def switch_frame(self):
        UtilsDriver.get_driver().switch_to.frame(self.goods_detail_page.find_iframe())

    #获取提示信息
    @allure.step(title="获取提示信息")
    def get_msg(self):
        return self.goods_detail_page.find_iframe_msg().text

# 定义业务层
class GoodsDetailProxy():
    def __init__(self):
        self.goods_detail_handle = GoodsDetailHandle()

    def add_card(self):
        self.goods_detail_handle.click_search_add_btn()
        self.goods_detail_handle.click_add_btn()
        self.goods_detail_handle.switch_frame()
        return self.goods_detail_handle.get_msg()